package com.dz.xjd.ui.presenter;

import com.dz.xjd.app.App;
import com.dz.xjd.base.baserx.RxSubscriber;
import com.dz.xjd.bean.ResponseResult;
import com.dz.xjd.bean.UserBean;
import com.dz.xjd.ui.contract.LoginContract;
import com.dz.xjd.utils.EncryptDecrypt;
import com.dz.xjd.utils.GsonUtil;
import com.dz.xjd.utils.KeyUtil;
import com.dz.xjd.utils.ToastUtil;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

public class LoginPresenter extends LoginContract.Presenter{
    @Override
    public void getLoginRequest(String header, String params) {
        mModel.getLoginData(header, params)
                .subscribe(new RxSubscriber<String>(mContext,true){
                    @Override
                    protected void _onNext(String data) {
                        Logger.d("data---"+data);
                        ResponseResult responseResult = GsonUtil.jsonToBean(data,ResponseResult.class);
                        int code = responseResult.getCode();
                        if(code == 0){
                            String[] keys = KeyUtil.getKey(App.getToken());
                            try {
                                String userInfo = EncryptDecrypt.decrypt(keys[0],responseResult.getData(),keys[1]);
                                Logger.d("getLoginRequest---"+userInfo);
                                UserBean userBean = GsonUtil.jsonToBean(userInfo,UserBean.class);
                                Logger.d("userBean----"+new Gson().toJson(userBean));
                                App.mUserBean = userBean;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            ToastUtil.showShort("登录成功");
                        }else if(code == 2){
                            Logger.d("token 过期---");
                        }else{
                            Logger.d("其他---");
                        }
                        mView.returnLoginData(code);
                    }

                    @Override
                    protected void _onError(String message) {
                        ToastUtil.showShort(message);
                    }
                });
    }

    @Override
    public void sendValidateRequest(String header, String params) {
        mModel.sendValidate(header, params).subscribe(new RxSubscriber<String>(mContext,true){
            @Override
            protected void _onNext(String data) {
                Logger.d("data---"+data);
                ResponseResult responseResult = GsonUtil.jsonToBean(data,ResponseResult.class);
                int code = responseResult.getCode();
                if(code == 0){
                    String[] keys = KeyUtil.getKey(App.getToken());
                    try {
                        String str = EncryptDecrypt.decrypt(keys[0],data,keys[1]);
                        Logger.d("sendValidateRequest---"+str);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    ToastUtil.showShort("验证码发送成功");
                }else if(code == 2){
                    Logger.d("token 过期---");
                }else{
                    Logger.d("其他---");
                }
                    mView.returnsendValidateData(code);
            }

            @Override
            protected void _onError(String message) {
                ToastUtil.showShort(message);
            }
        });
    }
}
